/*-----------------------------------------------File Info------------------------------------------------
** File Name:               Spi_user.c  
** Last modified date:      2016.07.22
** Last version:            V0.1
** Description:             
**--------------------------------------------------------------------------------------------------------            
** Created date:            2016.09.07
** author:                  YLY
** Version:                 V0.1
** Descriptions:            Spi驱动
**--------------------------------------------------------------------------------------------------------
*/

/*************************************************************
* 	include 
* 	头文件	
*************************************************************/
#include "bspSPI.h"

void spiSetNss(tsCfgSpi *cfg, teSpiNss eNss) 	
{
	if(eNss == NSS_HI)
		gpio_bit_write(cfg->nssIo.gpioPeriph, cfg->nssIo.pin, 1);	
	else	
		gpio_bit_write(cfg->nssIo.gpioPeriph, cfg->nssIo.pin, 0);					
}

/*------------------SPI0初始化-------------------------
*	cfg->spi: 	SPIx(x=0,1)
*	cfg: tsCfgSpi
*	rate:	xxK			
*/
void spiInit(tsCfgSpi *cfg, uint32_t rate )
{
	uint32_t psc;
    spi_parameter_struct  spi_init_struct;

	gpioConfig(&cfg->nssIo);
	gpioConfig(&cfg->sckIo);
	gpioConfig(&cfg->misoIo);
	gpioConfig(&cfg->mosiIo);
	spiSetNss(cfg, NSS_HI);
	
	//计算预分频
	psc = rcu_clock_freq_get(CK_AHB)/rate;	//72000000(72M)/1000000(1M)=72 -> <128 -> 72000000/128 = 562500(562.5K)
	if(psc <= 2)
		psc = SPI_PSC_2;
	else if(psc <= 4)
		psc = SPI_PSC_4;
	else if(psc <= 8)
		psc = SPI_PSC_8;
	else if(psc <= 16)
		psc = SPI_PSC_16;
	else if(psc <= 32)
		psc = SPI_PSC_32;	
	else if(psc <= 64)
		psc = SPI_PSC_64;	
	else if(psc <= 128)
		psc = SPI_PSC_128;	
	else if(psc <= 256)
		psc = SPI_PSC_256;	
	
	spi_struct_para_init(&spi_init_struct);		
	/* SPI0 parameter config */
	spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
	spi_init_struct.device_mode = cfg->masterSalve;
	spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
	
	if(cfg->eWorkMode == SPI_MODE0)
		spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;	//空闲CLK低 ,第一个时钟跳变沿采集第一个数据
	if(cfg->eWorkMode == SPI_MODE1)
		spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_1EDGE; //空闲CLK高 ,第一个时钟跳变沿采集第一个数据
	if(cfg->eWorkMode == SPI_MODE2)
		spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_2EDGE;	//空闲CLK低 ,第二个时钟跳变沿采集第一个数据
	else
		spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;	//空闲CLK高 ,第二个时钟跳变沿采集第一个数据

	spi_init_struct.nss = SPI_NSS_HARD;	//SPI_NSS_SOFT;	//SPI_NSS_HARD;	
	spi_init_struct.endian = SPI_ENDIAN_MSB;
	spi_init_struct.prescale = psc;
	
	rcu_periph_clock_enable(cfg->spiRcu);	
		
	/* deinitilize SPI and the parameters */
	spi_i2s_deinit(cfg->spi);
	
	spi_init(cfg->spi, &spi_init_struct);
	
    /* configure SPI1 byte access to FIFO */
    //spi_fifo_access_size_config(cfg->spi, SPI_BYTE_ACCESS);	
    spi_nss_output_enable(cfg->spi);    
    /* SPI NSSP mode enable */
    spi_nssp_mode_enable(cfg->spi);		
	/* SPI enable */
	spi_enable(cfg->spi);
}



/***************************************************************/                                         
/* SPI0写入一BYTE数据并读回一BYTE数据                      */                                                                                                                                                                
/***************************************************************/ 

uint8_t spiWriteReadByte(tsCfgSpi *cfg, uint8_t wrtieByte)
{
	while(RESET == spi_i2s_flag_get(cfg->spi, SPI_FLAG_TBE));
	spi_i2s_data_transmit(cfg->spi, wrtieByte);  	/* Write one byte to SPI flash */
	
	while(RESET == spi_i2s_flag_get(cfg->spi, SPI_FLAG_RBNE));	
	return spi_i2s_data_receive(cfg->spi);
}  

/*********************************************************************************************************



*********************************************************************************************************/
uint8_t spiSetRegister(tsCfgSpi *cfg, uint8_t regAddr, uint8_t regVal)
{	
	spiSetNss(cfg, NSS_LOW);
	
	spiWriteReadByte(cfg, regAddr<<1);
	spiWriteReadByte(cfg, regVal);
	
	spiSetNss(cfg, NSS_HI);
	return true;
}

/*********************************************************************************************************
*********************************************************************************************************/
uint8_t spiGetRegister(tsCfgSpi *cfg, uint8_t regAddr)
{
    uint8_t regVal;

    spiSetNss(cfg, NSS_LOW);
	
	spiWriteReadByte(cfg, 0x80|(regAddr<<1));
	
	regVal=spiWriteReadByte(cfg, 0x00);
	
    spiSetNss(cfg, NSS_HI);
    return regVal;
}






